package com.zjj.algorithm.learning.leetcode.tree;

import java.util.*;

/**
 * 103. 二叉树的锯齿形层序遍历
 * 给你二叉树的根节点 root ，返回其节点值的 锯齿形层序遍历 。（即先从左往右，再从右往左进行下一层遍历，以此类推，层与层之间交替进行）。
 * <p>
 * 输入：root = [3,9,20,null,null,15,7]
 * 输出：[[3],[20,9],[15,7]]
 * 示例 2：
 * <p>
 * 输入：root = [1]
 * 输出：[[1]]
 * 示例 3：
 * <p>
 * 输入：root = []
 * 输出：[]
 * <p>
 * <p>
 * 提示：
 * <p>
 * 树中节点数目在范围 [0, 2000] 内
 * -100 <= Node.val <= 100
 *
 * @author zjj_admin
 * @date 2022/12/5 10:48
 */
public class BinaryTreeZigzagLevelOrderTraversal {

    public static void main(String[] args) {

        /**
         *
         * [[3],[20,9],[15,7]]
         *
         *         3
         *      9     20
         *          15   7
         */
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        TreeNode node8 = new TreeNode(8);
        TreeNode node9 = new TreeNode(9);
        TreeNode node10 = new TreeNode(10);
        TreeNode node11 = new TreeNode(11);
        node1.left = node2;
        node1.right = node3;
        node3.left = node4;
        node3.right = node5;
        node4.left = node6;
        node4.right = node7;
        node5.left = node8;
        node5.right = node9;

        node2.left = node10;
        node2.right = node11;

        List<List<Integer>> res = zigzagLevelOrder(node1);
        System.out.println("res = " + res);

    }

    /**
     * 时间
     * 1 ms
     * 击败
     * 70.6%
     * 内存
     * 39.9 MB
     * 击败
     * 90.3%
     *
     * @param root
     * @return
     */
    public static List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        //设置一个标识 1 标识正序【从左向右】，2 标识逆序【从右向左】
        int flag = 1;
        if (root == null) {
            return res;
        }
        stack2.add(root);
        List<Integer> list1 = new ArrayList<>();
        list1.add(root.val);
        res.add(list1);
        while (!stack2.isEmpty() || !stack1.empty()) {
            List<Integer> list = new ArrayList<>();
            //当flag为1 时，标识从左向右，从队列中取值
            if (flag == 1) {
                while (!stack2.isEmpty()) {
                    TreeNode pop = stack2.pop();
                    if (pop.right != null) {
                        stack1.push(pop.right);
                        list.add(pop.right.val);
                    }
                    if (pop.left != null) {
                        stack1.push(pop.left);
                        list.add(pop.left.val);
                    }
                }
                flag = 2;
            } else {
                while (!stack1.isEmpty()) {
                    TreeNode pop = stack1.pop();
                    if (pop.left != null) {
                        stack2.add(pop.left);
                        list.add(pop.left.val);
                    }
                    if (pop.right != null) {
                        stack2.add(pop.right);
                        list.add(pop.right.val);
                    }
                }
                flag = 1;
            }
            if (!list.isEmpty()) {
                res.add(list);
            }
        }
        return res;
    }
}
